#include "Config.h"
#include <fstream>
#include <sstream>

using namespace std;

Config::Config(std::string FileName) {
	LoadConfig(FileName);
}

void Config::LoadConfig(std::string FileName) {
	ifstream f(FileName);

	if(!f.is_open())
		return;

	string Line;
	stringstream Lines;
	while(!f.eof()) {
		std::getline(f, Line);
		stringstream Lines(Line);

		if(Line.substr(0, 2) != "\\"	// If this line is NOT a comment
			&& Line.length() > 0) {		// And it is NOT empty
			Parameter p;

			f >> p.m_Name;
			f >> p.m_Var;

			m_Parameters.push_back(p);
		}
	}

	f.close();
}

int Config::GetInt(string ParamName, int Default) {
	int a = Default;
	for each(Parameter p in m_Parameters) {
		if(p.m_Name == ParamName)
			stringstream(p.m_Var) >> a;
	}
	return a;
}

bool Config::GetBool(string ParamName, bool Default) {
	int a = Default;
	for each(Parameter p in m_Parameters) {
		if(p.m_Name == ParamName)
			stringstream(p.m_Var) >> a;
	}
	return a;
}

float Config::GetFloat(string ParamName, float Default) {
	float a = Default;
	for each(Parameter p in m_Parameters) {
		if(p.m_Name == ParamName)
			stringstream(p.m_Var) >> a;
	}
	return a;
}

string Config::GetString(string ParamName, string Default) {
	for each(Parameter p in m_Parameters) {
		if(p.m_Name == ParamName)
			return p.m_Var;
	}
	return Default;
}

/*
class Config {
	struct Parameter {
	public:
		std::string m_Name;
		std::string m_Var;
	};

	std::vector<Parameter> m_Parameters;

public:
	Config(std::string FileName = NULL);

	void LoadConfig(std::string FileName);
	int GetInt(std::string ParamName);
	bool GetBool(std::string ParamName);
	float GetFloat(std::string ParamName);
	std::string GetString(std::string ParamName);
};
*/